﻿using System;
using System.Collections.Generic;
using System.Text;

namespace IllustratedCsharp7_ch18Generic
{
    /*扩展方法，允许将类中的静态方法关联到不同的泛型类上
    必须声明为static
    必须是静态类成员
    第一个参数类型中必须有关键字this，后面是扩展的泛型类的名字
     */
    static class ExtendHlder
    {
        /// <summary>
        /// 扩展了Holder<typeparamref name="T"/>的泛型类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="h"></param>
        public static void Print<T>(this Holder<T> h)
        {
            T[] vals = h.GetValus();
            Console.WriteLine($"{vals[0]},\t{vals[1]},\t{vals[2]}");
        }
    }
    class Holder<T>
    {
        T[] Vals = new T[3];
        public Holder(T v0, T v1, T v2)
        {
            Vals[0] = v0;Vals[1] = v1;Vals[2] = v2;
         }
        public T[] GetValus() { return Vals; }
    }
    class Chapt10_6ExtensionMethodsAndGenericClasses
    {
        static void _Main()
        {
            var intHolder = new Holder<int>(3, 5, 7);
            var stringHolder = new Holder<string>("a1", "b2", "c3");

            intHolder.Print();
            stringHolder.Print();
        }
    }
}
